//
//  MStatus.swift
//  iOSweibo
//
//  Created by lzm on 16/5/17.
//  Copyright © 2016年 lzm. All rights reserved.
//

import UIKit

class MStatus: NSObject {
    //微博创建时间
    var created_at : String?
    //微博ID
    var id : Int64 = 0
    //微博信息
    var text : String?
    //微博来源
    var source : String?
    //转发数
    var reposts_count : Int64 = 0
    //评论数
    var comments_count : Int64 = 0
    //微博表态数
    var attitudes_count : Int64 = 0
    
    //微博配图
    var pic_urls : [[String : AnyObject]]?{
        didSet{
            guard let urlDict = pic_urls else{
                MPrint(items: "没有配图")
                return
            }
            pictureURLs = [NSURL]()
            for dict in urlDict {
                if let urlStr = dict["thumbnail_pic"] as? String{
                    let url = NSURL(string: urlStr)
                    pictureURLs?.append(url!)
                }
            }
        }
    }
    var pictureURLs: [NSURL]?
    //定义用户模型
    var user : MUser?
    
    //转模型
    init(dict : [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
        
    }
    //将user字典转模型
    override func setValue(value: AnyObject?, forKey key: String) {
        if  key == "user"
        {
            if let dict = value as? [String:AnyObject]
            {
//                user = MUser(dict: dict)
                user = MUser(dict: dict)
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    override var description: String {
        get {
            
            // 使用每一个key去模型中找到值,使用key和找到的值,拼接成字典
            // keys是字典的key
            let keys = ["created_at", "id", "text", "source", "reposts_count", "comments_count", "attitudes_count", "pic_urls", "user"]
            
            return "\n \t 微博模型: \(dictionaryWithValuesForKeys(keys))"   // \n 换行 \t table
        }
    }
}
